package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.RivalNature;
import com.prs.crm.service.crm.RivalNatureService;
import com.prs.crm.service.sys.LogService;

public class RivalNatureAction extends BaseAction {
	private RivalNature rivalNature;
	private RivalNatureService rivalNatureService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listRivalNature")
	public String listRivalNature() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RivalNature.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalNatureService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editRivalNature")
	public String editRivalNature() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveRivalNature", results = {
			@Result(name = "success", type = "redirect", location = "listRivalNature"),
			@Result(name = "error", type = "chain", location = "editRivalNature") })
	public String saveRivalNature() {
		if (rivalNature.getNature() == null
				|| "".equals(rivalNature.getNature().trim())) {
			this.addActionError("企业性质不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName1(RivalNature.class, "nature", rivalNature
				.getNature())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}

		this.getRivalNatureService().save(rivalNature);
		this.setPrompt("提交竞争对手企业性质成功");
		return SUCCESS;
	}

	@Action(value = "removeRivalNature", results = {
			@Result(name = "success", type = "redirect", location = "listRivalNature"),
			@Result(name = "error", type = "chain", location = "listRivalNature") })
	public String removeRivalNature() {
		if (ids == null) {
			this.addActionError("请选择要删除的对手企业性质");
			return ERROR;
		}
		for (Integer id : ids) {
			RivalNature nature = this.getRivalNatureService().get(id);
			if (nature.getRivals().size() != 0) {
				this.addActionError("已有竞争对手引用，不能删除");
				return ERROR;
			}
		}
		this.getRivalNatureService().batchRemove(ids);
		return SUCCESS;
	}

	public RivalNature getRivalNature() {
		return rivalNature;
	}

	public void setRivalNature(RivalNature rivalNature) {
		this.rivalNature = rivalNature;
	}

	public RivalNatureService getRivalNatureService() {
		return rivalNatureService;
	}

	public void setRivalNatureService(RivalNatureService rivalNatureService) {
		this.rivalNatureService = rivalNatureService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
